package com.linzm.creationaltype.factory01.shapefactory;

import com.linzm.creationaltype.factory01.Shape.Circle;
import com.linzm.creationaltype.factory01.Shape.Rectangle;
import com.linzm.creationaltype.factory01.Shape.Shape;
import com.linzm.creationaltype.factory01.Shape.Square;
import com.linzm.creationaltype.factory01.enums.ShapeTypeEnum;
import com.linzm.creationaltype.factory01.enums.ShapeTypeInterface;

/**
 * @Author zimingl
 * @Date 2023/4/15 12:29
 * @Description: 图形工厂
 */
public class ShapeFactory extends AbstractShapeFactory {
    /**
     * 使用 getShape 方法获取形状类型的对象
     *
     * @param shapeType shapeType
     * @return Shape
     */
    @Deprecated
    public Shape getShape(String shapeType) {
        if (shapeType == null) {
            return null;
        }
        if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new Square();
        }
        return null;
    }

    /**
     * 获取 Shape 对象
     *
     * @param shapeType shapeType
     * @return Shape
     */
    public Shape getShapeInstance(String shapeType) throws Exception {
        ShapeTypeInterface shapeTypeEnum = ShapeTypeEnum.valueOf(shapeType);
        Class<?> clazz = shapeTypeEnum.getClazz();
        return (Shape) clazz.newInstance();
    }
}
